Telegram Group & Telegram Channel
📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/golang_books/975
Create:
Last Update:

📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books

BY Golang Books


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_books/975

View MORE
Open in Telegram


Golang Books Telegram | DID YOU KNOW?

Date: |

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

Golang Books from hk


Telegram Golang Books
FROM USA